package com.xcity.game.formation;

import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.combat.CombatException;
import com.xcity.game.common.MessageConst;
import com.xcity.game.event.GameEvents;
import com.xcity.pomelo.PomeloRoute;
import com.xcity.pomelo.request.CombatFormationChangeRequest;
import com.xcity.pomelo.request.CombatFormationFastSetRequest;
import com.xcity.pomelo.request.CombatFormationRequest;
import com.xcity.pomelo.response.CombatFormationResponse;
import com.xcity.pomelo.response.HintResponse;
import com.xcity.util.SessionUtils;

import naga.x.App;
import naga.x.event.Event;
import naga.x.event.EventListener;
import naga.x.net.Session;
import naga.x.net.annotation.OPHandler;
import naga.x.net.impl.pomelo.annotation.PomeloOP;
import naga.x.service.Service;
import naga.x.service.ServiceException;

/**
 * 战斗阵型
 * @author yang.li
 *
 */
@OPHandler
public class FormationService implements Service, EventListener {

	@Override
	public void startup() throws ServiceException {
		App.getApp().getEventManager().register(this);
	}

	@Override
	public void shutdown() throws ServiceException {
	}

	@Override
	public String getId() {
		return getClass().getName();
	}
	
	@Override
	public int[] getEventTypes() {
		return new int[] { GameEvents.EVENT_EMPLOYEE_COMBAT_POWER_CHANGED };
	}

	@Override
	public void handleEvent(Event event) {
		fireEmployeeCombatPowerChanged(event.getParameter(0));
	}
	
	private void fireEmployeeCombatPowerChanged(Employee employee) {
		Player player = employee.getVm().getOwner();
		player.getFormations().fireCombatPowerChanged();
	}

	@PomeloOP(PomeloRoute.COMBAT_FORMATION)
	protected void requestFormation(Session session, CombatFormationRequest req) {
		Player player = session.getClient();
		byte type = req.getType();
		Formation f = player.getFormations().get(type);
		long[] eids = f != null ? f.getIds() : null;
		session.send(new CombatFormationResponse(req.getSerial(), type, eids));
	}
	
	@PomeloOP(PomeloRoute.COMBAT_FORMATION_CHANGE)
	protected void requestFormationChange(Session session, CombatFormationChangeRequest req) {
		Player player = session.getClient();
		byte type = req.getType();
		int pos = req.getPos();
		try {
			Employee employee = null;
			if (req.getId() != 0L) {
				employee = player.getEmployees().get(req.getId());
				if (employee == null) {
					throw new CombatException(MessageConst.EMPLOYEE_NOT_EXIST);
				}
			}
			Formation f = player.getFormations().get(type);
			if (f == null) {
				throw new CombatException(MessageConst.ILLEGAL_OPERATION);
			}
			long[] ids = f.replace(pos, employee);
//			if (type == Formation.TYPE_NORMAL) {
//				player.fireAttributesChanged();
//			}
			session.send(new CombatFormationResponse(req.getSerial(), type, ids));
			App.getApp().getEventManager().addEvent(GameEvents.EVENT_FORMATION_CHANGED, player, f);
		} catch (CombatException e) {
			session.send(new HintResponse(req.getSerial(), e.getMessage()));
		}
	}
	
	@PomeloOP(PomeloRoute.COMBAT_FORMATION_FAST_SET)
	protected void requestFastFormation(Session session, CombatFormationFastSetRequest req) {
		if (SessionUtils.isOperateTooFrequent(session, SessionUtils.LAST_FORMATION_FAST_SET_TIME)) {
			session.send(new HintResponse(req.getSerial(), MessageConst.OPERATE_TOO_FREQUENT));
			return;
		}
		Player player = session.getClient();
		byte type = req.getType();
		Formation f = player.getFormations().get(type);
		if (f == null) {
			session.send(new HintResponse(req.getSerial(), MessageConst.ILLEGAL_OPERATION));
			return;
		}
		long[] eids = player.getFormations().fastSet(f);
//		if (type == Formation.TYPE_NORMAL) {
//			player.fireAttributesChanged();
//		}
		session.send(new CombatFormationResponse(req.getSerial(), type, eids));
		App.getApp().getEventManager().addEvent(GameEvents.EVENT_FORMATION_CHANGED, player, f);
	}

}
